package com.mengxuegu.springboot.controller;

import com.mengxuegu.springboot.entities.CardBean;
import com.mengxuegu.springboot.entities.UsersBean;
import com.mengxuegu.springboot.mapper.BankCardMapper;
import com.mengxuegu.springboot.mapper.UserMapper;
import com.mengxuegu.springboot.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {


    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserMapper userMapper;

    @Autowired
    BankCardMapper bankCardMapper;



    @PostMapping("/Iname")
    public Object getByUserIname(@RequestBody UsersBean bean){

        return  userMapper.getByUserIname(bean.getIname());

    }

    @PostMapping("/add")
    public Object addUser(@RequestBody UsersBean usersBean){

        int i = userMapper.addUser(usersBean);
        if(i>0){
            return true;
        }
        else {
            return false;
        }
    }

    @PostMapping("/lgin")
    public String login(HttpSession httpSession,String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(password)){
            UsersBean usersBean = userMapper.getByUserIname(username);
//
//            String md5 = MD5Util.getMD5(usersBean.getPwd());
            if(usersBean!=null&&password.equals(usersBean.getPwd())){

                httpSession.setAttribute("loguser",usersBean.getIname());
                return "main/system";
            }

        }

        return "main/error";
    }

    @GetMapping("/cardlook")
    public String cardlook(Map<String,Object> map){

        List<CardBean> card = bankCardMapper.getCardAll();
        map.put("cardlook",card);
        logger.info("+++"+card);
        return "card/cardlook";

    }


}
